package com.ddwl.user.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.user.dao.mapper.ProfitRegionMapper;
import com.ddwl.user.dao.model.ProfitRegion;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * 类ProfitApplyDao.java的实现描述：DAO
 *
 * @author zach
 * @Date :2020-06-23 10:48.
 */
@Repository
public class ProfitRegionDao {
    @Resource
    private ProfitRegionMapper profitRegionMapper;


    public List<ProfitRegion> getProfitRegion(){
        ProfitRegion profitRegion = new ProfitRegion();
        profitRegion.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return profitRegionMapper.select(profitRegion);
    }

    /**
     * 清空用户区域
     * @param userId
     */
    public void deleteProfitRegion(String userId) {
        ProfitRegion profitRegion = new ProfitRegion();
        profitRegion.setUserId(userId);
        profitRegionMapper.delete(profitRegion);
    }

    public List<ProfitRegion>  getProfitRegion(String userId) {
        ProfitRegion profitRegion = new ProfitRegion();
        profitRegion.setUserId(userId);
        profitRegion.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return profitRegionMapper.select(profitRegion);
    }

    /**
     * 查询该区域绑定 的用户
     * @param regionId
     * @return
     */
    public List<ProfitRegion> getProfitRegionByRegion(String regionId) {
        ProfitRegion profitRegion = new ProfitRegion();
        profitRegion.setRegionId(regionId);
        profitRegion.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return profitRegionMapper.select(profitRegion);
    }

    /**
     * 区域信息
     * @param regionIds
     * @return
     */
    public List<ProfitRegion> getProfitRegionByRegion(List<String> regionIds) {
        Example example = new Example(ProfitRegion.class);
        example.and().andIn("regionId",regionIds)
                .andEqualTo("dflag",BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return profitRegionMapper.selectByExample(example);
    }

    /**
     * 查询 当前区域被绑定信息 排除当前用户
     * @param regionIds
     * @return
     */
    public List<ProfitRegion> getProfitRegionByRegionNotUserId(List<String> regionIds,String userId) {
        Example example = new Example(ProfitRegion.class);
        example.and().andIn("regionId",regionIds)
                .andNotEqualTo("userId",userId)
                .andEqualTo("dflag",BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return profitRegionMapper.selectByExample(example);
    }


    public void addProfitRegion(List<String> regionIds, String userId) {
        List<ProfitRegion>  profitRegions = new ArrayList<>();
        for (String regionId : regionIds) {
            ProfitRegion profitRegion = new ProfitRegion();
            profitRegion.setGmtModified(Instant.now());
            profitRegion.setGmtCreated(Instant.now());
            profitRegion.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
            profitRegion.setUserId(userId);
            profitRegion.setRegionId(regionId);
            profitRegions.add(profitRegion);
        }
        profitRegionMapper.insertList(profitRegions);
    }
}
